package com.tiger.project.service.impl;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.tiger.common.core.redis.RedisCache;
import com.tiger.common.utils.Code;
import com.tiger.common.utils.DateUtils;
import com.tiger.project.domain.Asset;
import com.tiger.project.domain.MaintenanceApply;
import com.tiger.project.domain.PatrolRecord;
import com.tiger.project.mapper.AssetMapper;
import com.tiger.project.mapper.MaintenanceApplyMapper;
import com.tiger.project.mapper.PatrolRecordMapper;
import com.tiger.project.service.IPatrolRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【巡查记录】Service业务层处理
 *
 * @author standard
 * @date 2022-09-16
 */
@Service
public class PatrolRecordServiceImpl implements IPatrolRecordService {
    @Autowired
    private PatrolRecordMapper patrolRecordMapper;
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private MaintenanceApplyMapper applyMapper;

    /**
     * 查询【巡查记录】
     *
     * @param id 【巡查记录】主键
     * @return 【巡查记录】
     */
    @Override
    public PatrolRecord selectPatrolRecordById(Long id) {
        PatrolRecord patrolRecord = patrolRecordMapper.selectPatrolRecordById(id);
        if (StringUtils.isNotEmpty(patrolRecord.getPictures()))
            patrolRecord.setPicturesList(Arrays.asList(patrolRecord.getPictures().split(",")));

        // 资产
        Asset asset = new Asset();
        asset.setId(patrolRecord.getAssetId());
        List<Asset> list = assetMapper.selectAssetList(asset);
        if (list != null && list.size() > 0){
            patrolRecord.getAssetList().addAll(list);
        }

        //维修项目
        MaintenanceApply maintenanceApply = new MaintenanceApply();
        maintenanceApply.setPatrolRecordId(id);
        List<MaintenanceApply> maintenanceApplyList = applyMapper.selectMaintenanceApplyList(maintenanceApply);
        if (maintenanceApplyList !=null && maintenanceApplyList.size() > 0){
            patrolRecord.getApplyList().addAll(maintenanceApplyList);
        }

        return patrolRecord;
    }

    /**
     * 查询【巡查记录】列表
     *
     * @param patrolRecord 【巡查记录】
     * @return 【巡查记录】
     */
    @Override
    public List<PatrolRecord> selectPatrolRecordList(PatrolRecord patrolRecord) {
        return patrolRecordMapper.selectPatrolRecordList(patrolRecord);
    }

    /**
     * 新增【巡查记录】
     *
     * @param patrolRecord 【巡查记录】
     * @return 结果
     */
    @Override
    public int insertPatrolRecord(PatrolRecord patrolRecord) {
        int code = redisCache.getCacheObjectCode(LocalDate.now().format(Code.fmt) + "xc");
        addCode(code);
        patrolRecord.setPatrolRecordNo(String.format("%s%s%04d", "XC",LocalDate.now().format(Code.fmt), code));
        patrolRecord.setCreatedAt(DateUtils.getNowDate());
        patrolRecord.setIsDeleted(0L);
        return patrolRecordMapper.insertPatrolRecord(patrolRecord);
    }

    /**
     * 修改【巡查记录】
     *
     * @param patrolRecord 【巡查记录】
     * @return 结果
     */
    @Override
    public int updatePatrolRecord(PatrolRecord patrolRecord) {
        patrolRecord.setUpdatedAt(DateUtils.getNowDate());
        return patrolRecordMapper.updatePatrolRecord(patrolRecord);
    }

    /**
     * 批量删除【巡查记录】
     *
     * @param ids 需要删除的【巡查记录】主键
     * @return 结果
     */
    @Override
    public int deletePatrolRecordByIds(Long[] ids) {
        return patrolRecordMapper.deletePatrolRecordByIds(ids);
    }

    /**
     * 删除【巡查记录】信息
     *
     * @param id 【巡查记录】主键
     * @return 结果
     */
    @Override
    public int deletePatrolRecordById(Long id) {
        return patrolRecordMapper.deletePatrolRecordById(id);
    }

    //审核回调
    @Override
    public int callBack(Long id,Long status) {
        PatrolRecord patrolRecord = patrolRecordMapper.selectPatrolRecordById(id);
        patrolRecord.setStatus(status);
        patrolRecord.setUpdatedAt(DateUtils.getNowDate());
        return patrolRecordMapper.updatePatrolRecord(patrolRecord);
    }


    /**
     * 生成编码
     */
    private synchronized void addCode(int code) {
        redisCache.setCacheObject(LocalDate.now().format(Code.fmt) + "xc", code + 1);
    }


}
